﻿package {
	import flash.display.Bitmap;
	import flash.display.PixelSnapping;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import idv.cjcat.pyronova.display.PyroView;
	import idv.cjcat.pyronova.nodes.Node;
	import net.hires.debug.Stats;
	
	public class PyronovaExample extends Sprite {
		
		[Embed(source = "assets/Arial.ttf", fontName="Arial Black", fontFamily="normal")]
		private var arial:Class;
		
		[Embed(source = "assets/Arial Black.ttf", fontName="Arial Black", fontFamily="black")]
		private var arialBlack:Class;
		
		[Embed(source = "assets/Pyronova logo small.png")]
		private var PyronovaLogo:Class;
		
		private var stats:Stats;
		
		protected var _view:PyroView;
		public function get view():PyroView { return _view; }
		
		public function PyronovaExample(title:String = "", backgroundColor:uint = 0xFFFFFF) {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			//build background
			var bg:Shape = new Shape();
			bg.graphics.beginFill(backgroundColor);
			bg.graphics.drawRect(0, 0, 640, 480);
			addChild(bg);
			
			//build view
			_view = new PyroView(320, 200);
			_view.root = buildRoot();
			_view.preprocessor = buildPreprocessor();
			_view.postprocessor = buildPostprocessor();
			var bmp:Bitmap = new Bitmap(_view.bitmapData(), PixelSnapping.ALWAYS, false);
			bmp.y = 40;
			bmp.scaleX = bmp.scaleY = 2;
			addChild(bmp);
			
			//build black rects
			var r1:Shape = new Shape(), r2:Shape = new Shape();
			r1.graphics.beginFill(0x000000);
			r1.graphics.drawRect(0, 0, 640, 40);
			r2.graphics.beginFill(0x000000);
			r2.graphics.drawRect(0, 0, 640, 60);
			r2.y = 440;
			addChild(r1);
			addChild(r2);
			
			//build stats
			stats = new Stats();
			stats.x = 2;
			stats.y = 42;
			addChild(stats);
			
			//build logo
			var logo:Sprite = new Sprite();
			logo.x = 6;
			logo.y = 362;
			logo.addChild(new PyronovaLogo());
			addChild(logo);
			
			
			//text
			//-------------------------------------------------
			
			//build title
			var titleTF:TextField = new TextField();
			titleTF.width = 640;
			titleTF.x = 4;
			titleTF.y = 3;
			titleTF.selectable = false;
			titleTF.type = TextFieldType.DYNAMIC;
			titleTF.text = title;
			titleTF.setTextFormat(new TextFormat("Arial Black", 20, 0xFFFFFF));
			addChild(titleTF);
			
			//build CJ
			var cjTF:TextField = new TextField();
			cjTF.width = 640;
			cjTF.x = 413;
			cjTF.y = 442;
			cjTF.selectable = false;
			cjTF.type = TextFieldType.DYNAMIC;
			cjTF.text = "2010 CJ Cat (Allen Chou)";
			cjTF.setTextFormat(new TextFormat("Arial", 14, 0xFFFFFF));
			addChild(cjTF);
			
			//build project homepage URL
			var pyronovaURL:TextField = new TextField();
			pyronovaURL.width = 640;
			pyronovaURL.x = cjTF.x;
			pyronovaURL.y = cjTF.y + 17;
			pyronovaURL.selectable = false;
			pyronovaURL.type = TextFieldType.DYNAMIC;
			pyronovaURL.htmlText = "<a href='http://code.google.com/p/pyronova'>http://code.google.com/p/pyronova</a>";
			pyronovaURL.setTextFormat(new TextFormat("Arial", 14, 0xFFFFFF));
			addChild(pyronovaURL);
			
			//build blog URL
			var blogURL:TextField = new TextField();
			blogURL.width = 640;
			blogURL.x = cjTF.x;
			blogURL.y = pyronovaURL.y + 17;
			blogURL.selectable = false;
			blogURL.type = TextFieldType.DYNAMIC;
			blogURL.htmlText = "<a href='http://cjcat.blogspot.com'>http://cjcat.blogspot.com</a>";
			blogURL.setTextFormat(new TextFormat("Arial", 14, 0xFFFFFF));
			addChild(blogURL);
			
			//-------------------------------------------------
			//end of text
			
			
			addEventListener(Event.ENTER_FRAME, listener);
		}
		
		private function listener(e:Event):void {
			mainLoop();
			_view.render();
		}
		
		protected function buildRoot():Node {
			return null;
		}
		
		protected function buildPreprocessor():Node {
			return null;
		}
		
		protected function buildPostprocessor():Node {
			return null;
		}
		
		protected function mainLoop():void {
			
		}
	}
}